package com.zhongfl.crm.service.impl.boss;

import com.zhongfl.crm.exception.CRMException;
import com.zhongfl.crm.lixiao.mapper.BankMapper;
import com.zhongfl.crm.lixiao.mapper.RelationAmibaBankMapper;
import com.zhongfl.crm.model.boss.Bank;
import com.zhongfl.crm.model.boss.RelationAmibaBank;
import com.zhongfl.crm.service.boss.RelationAmibaBankService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import tech.heptagram.channel.api.common.AssertUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * RelationAmibaBankServiceImpl 阿米巴-银行 关系服务
 *
 * @author tiantian
 * @date 2018/8/10 16:14
 */
@Service
public class RelationAmibaBankServiceImpl implements RelationAmibaBankService {
    @Resource
    private RelationAmibaBankMapper relationAmibaBankMapper;
    @Resource
    private BankMapper bankMapper;

    @Override
    public List<RelationAmibaBank> findBankListByAmibaId(Integer amibaId) {
        AssertUtils.notNull(amibaId, "阿米巴ID为空！！");
        return relationAmibaBankMapper.selectBankListByAmibaId(amibaId);
    }

    @Override
    public List<RelationAmibaBank> findAvailableBankListByAmibaId(Integer id) {
        List<RelationAmibaBank> bankListByAmibaId = this.findBankListByAmibaId(id);

        List<RelationAmibaBank> collect = Collections.emptyList();
        if(CollectionUtils.isNotEmpty(bankListByAmibaId)){
            collect = bankListByAmibaId.stream().filter(item -> item.getStatus() == 1).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public RelationAmibaBank findByRelationAmibaBankId(Integer relationId) {
        AssertUtils.notNull(relationId,"关系ID为空！");
        return relationAmibaBankMapper.selectByRelationAmibaBankId(relationId);
    }

    @Override
    public void updateByRelationAmibaBankId(RelationAmibaBank relationAmibaBank) {
        AssertUtils.notNull(relationAmibaBank, "更新对象为空！");

        relationAmibaBankMapper.updateByIdSelective(relationAmibaBank);
    }

    @Override
    public void saveRelationAmibaBank(Integer amibaId, Integer bankId, Integer parentId, Integer status,String createdBy) {
        AssertUtils.notNull(amibaId,"阿米巴ID为空！");
        AssertUtils.notNull(bankId,"银行ID为空！");
        AssertUtils.notNull(parentId,"银行父级ID为空！");
        AssertUtils.notNull(status,"状态为空！");

        Bank bank = bankMapper.selectByIdAndParentId(bankId,parentId);

        if(Objects.isNull(bank)){
            throw new CRMException("所选分行和支行没有关系！");
        }

        RelationAmibaBank relationAmibaBank = new RelationAmibaBank();
        relationAmibaBank.setAmibaId(amibaId);
        relationAmibaBank.setBankId(bankId);
        relationAmibaBank.setStatus(status);
        relationAmibaBank.setCreatedBy(createdBy);
        relationAmibaBank.setModifiedBy(createdBy);
        relationAmibaBankMapper.insertSelective(relationAmibaBank);
    }
}
